package com.dcy.znoa.service.attendance.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.DateUtil;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.attendance.model.attRecord.PunchModel;
import com.dcy.znoa.controller.publiz.model.oper.SelectOperModel;
import com.dcy.znoa.dao.attendance.AttRealRecordDao;
import com.dcy.znoa.dao.attendance.AttRecordDao;
import com.dcy.znoa.dao.attendance.AttRuleDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.attendance.AttRealRecord;
import com.dcy.znoa.domain.attendance.AttRecord;
import com.dcy.znoa.domain.attendance.AttRule;
import com.dcy.znoa.domain.sys.Role;
import com.dcy.znoa.service.attendance.AttRecordService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class AttRecordServiceImpl implements AttRecordService {
    @Autowired
    AttRecordDao attRecordDao;
    @Autowired
    AttRealRecordDao attRealRecordDao;
    @Autowired
    OperDao operDao;
    @Autowired
    AttRuleDao attRuleDao;

    @Override
    public List selectList() throws InvocationTargetException, IllegalAccessException {
        return null;
    }

    @Override
    public Integer insert(Role role) {
        return null;
    }

    @Override
    public Integer updateById(Role role) {
        return null;
    }

    @Override
    public List queryListMapper(SelectOperModel model) {
        return null;
    }

    @Transactional
    @Override
    public Integer punch(PunchModel model) throws InvocationTargetException, IllegalAccessException, BusinessException {
        //查询，如果有默认的考勤规则，就把用户做关联
        Long operId = model.getOperId();
        Oper user = operDao.selectById(operId);
        AttRule attRule = new AttRule();
        EntityWrapper<AttRule> ew = new EntityWrapper();
        ew.setEntity(attRule);
        ew.eq("comp_id",user.getCompId()).eq("is_default",1);
        List<AttRule> attRuleList = attRuleDao.selectList(ew);
        if(!attRuleList.isEmpty()){
            user.setAttRuleNo(attRuleList.get(0).getAttRuleNo());
        }
        operDao.updateById(user);
//        //查询今天在有效记录中的数据
        String nowYMD = DateUtil.getNowDateYMD();
        String st = " 00:00:00";//
        String et = " 23:59:59";

        //如果已经有申请，就不准打卡
        List<AttRealRecord> applyList = attRealRecordDao.selectList(
                new EntityWrapper<AttRealRecord>().eq("att_time",nowYMD).eq("time_status",1).eq(("record_type"),model.getRecordType()).eq(("oper_id"),model.getOperId()));
        if(!applyList.isEmpty()){
            throw new BusinessException("404","已经有申请，无需打卡");
        }
        List<AttRealRecord> list = attRealRecordDao.selectList(
                new EntityWrapper<AttRealRecord>().ge("record_time",nowYMD+st).le("record_time",nowYMD+et).eq(("record_type"),model.getRecordType()).eq(("oper_id"),model.getOperId()));
        for(AttRealRecord attRealRecord : list){
            //删除AttRealRecord中老的
            attRealRecordDao.deleteById(attRealRecord.getRecordId());
        }
        AttRecord cp = new AttRecord();
        BeanUtils.copyProperties(cp, model);
        //暂时由前端验证 时间 地点 是否异常
        attRecordDao.insert(cp);
        AttRealRecord cpr = new AttRealRecord();
        BeanUtils.copyProperties(cpr, cp);
        attRealRecordDao.insert(cpr);
//        //如果正常 且没有真实的考勤记录，就放到有效考勤表中
//        if(list.isEmpty()){
//            BeanUtils.copyProperties(cpr, cp);
//            attRealRecordDao.insert(cpr);
//            return 1;
//        }else if(list.size()>1){//每天的上下班记录只能各有一条
//            throw new BusinessException("404","用户ID"+ model.getOperId()+"在有效记录表中数据有误，请联系管理员");
//        }else{
//            AttRealRecord arr = list.get(0);
//            //如果arr有效，那么什么都不做
//            //arr无效 ar 有效 删除arr ,ar 复制成 arr insert
//            //arr无效 ar 无效 什么都不做
//            if((arr.getAddStatus()==0 ||arr.getTimeStatus() ==0) && (cp.getAddStatus()==1 && cp.getTimeStatus()==1)){
//                attRealRecordDao.deleteById(arr.getRecordId());
//                BeanUtils.copyProperties(cpr, cp);
//                attRealRecordDao.insert(cpr);
//                return 1;
//            }
//        }
        return 1;
    }

}
